// pages/modifyProject/modifyProject.js
let dateTimePicker = require('../../utils/getPickerTime.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectName: '',
    performerVal: '服务器加载数据',
    makeUpVal: '服务器加载数据',
    photographyVal: '服务器加载数据',
    fieldVal: '服务器加载数据',
    description: '',
    startDate: '',
    endDate: '',
    dateTime1: null,
    dateTime2: null,
    dateTimeArray1: null,
    dateTimeArray2: null,
    chargeName: '',
    performerIds: [],
    makeUpIds: [],
    photographyIds: [],
    fieldIds: [],
    projectStatus: null,
    projectId: null,
  },
  changeInput: function(e) {
    // 数据双向绑定
    let dataset = e.currentTarget.dataset
    let value = e.detail.value
    this.data[dataset.obj] = value
    this.setData({
      obj: this.data[dataset.obj]
    })
    // 缓存
    if (dataset.obj == 'projectName') {
      wx.setStorage({
        key: 'projectName',
        data: this.data[dataset.obj],
      })
    } else if (dataset.obj == 'description') {
      wx.setStorage({
        key: 'description',
        data: this.data[dataset.obj],
      })
    }
  },
  openPerformer: function() {
    // 打开网红
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch?status=newProject&cate=1',
    })
    wx.setStorage({
      key: 'navid',
      data: '1',
    })
  },
  openMakeUp: function() {
    // 打开化妆师
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch?status=newProject&cate=3',
    })
    wx.setStorage({
      key: 'navid',
      data: '3',
    })
  },
  openPhotography: function() {
    // 打开摄影师
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch?status=newProject&cate=2',
    })
    wx.setStorage({
      key: 'navid',
      data: '2',
    })
  },
  openField: function() {
    // 打开场地
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch?status=newProject&cate=4',
    })
    wx.setStorage({
      key: 'navid',
      data: '4',
    })
  },
  startDateChange: function(e) {
    // 修改起始时间
    let dateTimeArray1 = this.data.dateTimeArray1
    let dateTime1 = this.data.dateTime1
    let currentYear = new Date().getFullYear()
    let currentMonth = new Date().getMonth() + 1
    let currentDay = new Date().getDate()
    let currentHours = new Date().getHours()
    let currentMin = new Date().getMinutes()
    // let currentSec = new Date().getSeconds()
    let currentTime = currentYear + '-' + currentMonth + '-' + currentDay + ' ' + currentHours + ':' + currentMin
    let st = dateTimeArray1[0][dateTime1[0]] + '-' + dateTimeArray1[1][dateTime1[1]] + '-' + dateTimeArray1[2][dateTime1[2]] + '  ' + dateTimeArray1[3][dateTime1[3]] + ':' + dateTimeArray1[4][dateTime1[4]]

    let currentDate = new Date(currentTime.replace(/-/g, "/"))
    let startDate = new Date(st.replace(/-/g, "/"))

    // 判断起始时间不能早于当前时间
    if (currentDate > startDate) {
      wx.showToast({
        title: '起始时间不能早于当前时间',
        icon: 'none'
      })
    } else if (currentDate < startDate) {
      this.setData({
        dateTime1: e.detail.value,
        startDate: st
      })
    }
  },
  startDateChangeCol: function(e) {
    // 起始时间-改变列
    let arr = this.data.dateTime1
    let dateArr = this.data.dateTimeArray1

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  endDateChange: function(e) {
    // 修改结束时间
    let dateTimeArray2 = this.data.dateTimeArray2
    let dateTime2 = this.data.dateTime2
    let startDate = this.data.startDate
    let st = dateTimeArray2[0][dateTime2[0]] + '-' + dateTimeArray2[1][dateTime2[1]] + '-' + dateTimeArray2[2][dateTime2[2]] + '  ' + dateTimeArray2[3][dateTime2[3]] + ':' + dateTimeArray2[4][dateTime2[4]]

    // 判断结束时间不能早于起始时间
    if (startDate == '请选择日期时间') {
      wx.showToast({
        title: '请先选择起始时间',
        icon: 'none'
      })
    } else {
      let sd = new Date(startDate.replace(/-/g, "/"))
      let ed = new Date(st.replace(/-/g, "/"))
      if (sd > ed) {
        wx.showToast({
          title: '结束时间不能早于起始时间',
          icon: 'none'
        })
      } else if (sd < ed) {
        this.setData({
          dateTime2: e.detail.value,
          endDate: st
        })
      }
    }
  },
  endDateChangeCol: function(e) {
    // 结束时间-改变列
    let arr = this.data.dateTime2
    let dateArr = this.data.dateTimeArray2

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },
  cancel: function() {
    // 取消修改项目
    wx.showModal({
      content: '是否放弃修改?',
      success(res) {
        if (res.confirm) {
          // 确定
          wx.navigateBack({
            delta: 1
          })
        } else if (res.cancel) {
          // 取消
          console.log('取消')
        }
      }
    })
  },
  sure: function() {
    // 确定修改
    let that = this
    let config = wx.getStorageSync('config')
    let productId = []
    // 网红
    if (that.data.performerIds.length > 0) {
      productId = productId.concat(that.data.performerIds)
    }
    // 化妆师
    if (that.data.makeUpIds.length > 0) {
      productId = productId.concat(that.data.makeUpIds)
    }
    // 摄影师
    if (that.data.photographyIds.length > 0) {
      productId = productId.concat(that.data.photographyIds)
    }
    // 场地
    if (that.data.fieldIds.length > 0) {
      productId = productId.concat(that.data.fieldIds)
    }
    wx.request({
      url: config.host + 'api/project/updateProject',
      method: 'POST',
      data: {
        content: that.data.description,
        endTime: that.data.endDate,
        id: that.data.projectId,
        principalName: that.data.chargeName,
        projectName: that.data.projectName,
        startTime: that.data.startDate,
        status: that.data.projectStatus,
        productId: productId,
      },
      success(res) {
        if (res.data.status == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }else if(res.data.status==400){
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
    setTimeout(() => {
      wx.redirectTo({
        url: '/pages/projectDetail/projectDetail?projectId=' + that.data.projectId,
      })
    }, 1000)
    that.clearStorage()
  },
  clearStorage: function() {
    // 清空表单缓存
    wx.removeStorageSync('performerNames')
    wx.removeStorageSync('performerIds')
    wx.removeStorageSync('dresserNames')
    wx.removeStorageSync('dresserIds')
    wx.removeStorageSync('photographerNames')
    wx.removeStorageSync('photographerIds')
    wx.removeStorageSync('fieldNames')
    wx.removeStorageSync('fieldIds')
    wx.removeStorageSync('projectName')
    wx.removeStorageSync('description')
  },
  getProjectData: function(projectId) {
    // 获取项目详情
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/projectInfo',
      method: 'GET',
      data: {
        projectId: projectId
      },
      success(res) {
        if (res.data.status == 200) {
          let participantArray = res.data.data.projectAgree

          let performerArr = participantArray.filter((item => {
            return item.type == 1
          }))
          let performerStr = performerArr.map((item) => {
            return item.productName
          }).join(',')
          let performerIds = performerArr.map((item => {
            return item.productId
          }))

          let makeUpArr = participantArray.filter(item => {
            return item.type == 3
          })
          let makeUpStr = makeUpArr.map((item) => {
            return item.productName
          }).join(',')
          let makeUpIds = makeUpArr.map((item => {
            return item.productId
          }))

          let photographyArr = participantArray.filter(item => {
            return item.type == 2
          })
          let photographyStr = photographyArr.map((item => {
            return item.productName
          }))
          let photographyIds = photographyArr.map((item => {
            return item.productId
          }))

          let fieldArr = participantArray.filter(item => {
            return item.type == 4
          })
          let fieldStr = fieldArr.map((item => {
            return item.productName
          }))
          let fieldIds = fieldArr.map((item => {
            return item.productId
          }))

          that.setData({
            projectName: res.data.data.projectName,
            chargeName: res.data.data.principalName,
            startDate: res.data.data.startTime,
            endDate: res.data.data.endTime,
            description: res.data.data.content,
            projectStatus: res.data.data.projectStatus,
            projectId: res.data.data.projectId,
            performerVal: performerStr,
            performerIds: performerIds,
            makeUpVal: makeUpStr,
            makeUpIds: makeUpIds,
            photographyVal: photographyStr,
            photographyIds: photographyIds,
            fieldVal: fieldStr,
            fieldIds: fieldIds
          })
        }
      }
    })
  },
  getDate: function() {
    // 获取时间
    let that = this
    // 获取完整的年月日 时分秒，以及默认显示的数组
    let obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    let lastArray = obj.dateTimeArray.pop();
    let lastTime = obj.dateTime.pop();
    this.setData({
      dateTimeArray1: obj.dateTimeArray,
      dateTimeArray2: obj.dateTimeArray,
      dateTime1: obj.dateTime,
      dateTime2: obj.dateTime
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    // 获取时间
    this.getDate()
    // 获取项目id
    let projectId = options.projectId
    that.getProjectData(projectId)
    // setStorage保存服务器项目详情
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    let performerNames = wx.getStorageSync('performerNames')
    let performerIds = wx.getStorageSync('performerIds')
    let dresserNames = wx.getStorageSync('dresserNames')
    let dresserIds = wx.getStorageSync('dresserIds')
    let photographerNames = wx.getStorageSync('photographerNames')
    let photographerIds = wx.getStorageSync('photographerIds')
    let fieldNames = wx.getStorageSync('fieldNames')
    let fieldIds = wx.getStorageSync('fieldIds')
    // 获取名字展示
    if (performerNames.length > 0) {
      that.setData({
        performerVal: performerNames.toString()
      })
    }
    if (dresserNames.length > 0) {
      that.setData({
        makeUpVal: dresserNames.toString()
      })
    }
    if (photographerNames.length > 0) {
      that.setData({
        photographyVal: photographerNames.toString()
      })
    }
    if (fieldNames.length > 0) {
      that.setData({
        fieldVal: fieldNames.toString()
      })
    }
    // 获取网红，化妆师，摄影师，场地id值
    if (performerIds.length > 0) {
      that.setData({
        performerIds: performerIds
      })
    }
    if (dresserIds.length > 0) {
      that.setData({
        makeUpIds: dresserIds
      })
    }
    if (photographerIds.length > 0) {
      that.setData({
        photographyIds: photographerIds
      })
    }
    if (fieldIds.length > 0) {
      that.setData({
        fieldIds: fieldIds
      })
    }
    // 获取项目名称和详细描述
    let projectName = wx.getStorageSync('projectName')
    let description = wx.getStorageSync('description')
    if (projectName != '') {
      that.setData({
        projectName: projectName
      })
    } else if (description != '') {
      that.setData({
        description: description
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.clearStorage()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})